/**
 * 客户负责人信息维护
 * @author liuw
 * @date 2019/11/06
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        table,
        element,
        vueData = {

        },
        table_where = {

        },
        cols = [[
            {type: 'checkbox', title: '选择', fixed: 'left'}
            ,{field:"ESAB001",title:"客户编号",width:120,search:"default",searchOpts:{icon:true}}
            ,{field:"ESAB030",title:"客户简称",width:120,search:"default"}
            ,{field:"ESAB002",title:"客户项目状态",width:100,
                filterDatas: app.datas_('problem_esai120_esab002'),search:'default', searchOpts: {type: 'select'}}
            ,{field:"ESAB003_NAME",title:"主要联系人",width:100}
            ,{field:"ESAB007",title:"e-mail",width:150}
            ,{field:"ESAB014",title:"产品线",width:100,hide:true}
            ,{field:"ESAB014_NAME",title:"产品线",width:120}
            ,{field:"ESAB029",title:"ERP产品线",width:100,filterDatas: app.datas_('problem_esai100_esaa006')}
            ,{field:"ESAB015",title:"数据库",width:100,
                filterDatas: app.datas_('problem_esai120_esab015')}
            ,{field:"ESAB025_NAME",title:"维保服务人员",width:100}

            , {field:"ESAB003",width:100,title:"主要联系人",hide:true,search:"default",searchOpts:{icon:true}}
            , {field:"ESAB008",width:100,hide:true}
            , {field:"ESAB009",width:100,hide:true}
            , {field:"ESAB010",width:100,hide:true}
            , {field:"ESAB025",width:100,title:"维保人员",hide:true,search:"default",searchOpts:{icon:true}}
            , {field:"ESAB026",width:100,hide:true}

            , {
                width: 60,
                title: '',
                toolbar: '#script-esai120setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        esai120SettingAction,
        moduleName = '客户负责人',
        editArea = ['480px', '430px']
    ;

    var esai120 = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'esai120',
                _searchs: [{
                    id: 'esai120', tableId: 'esai120_table', datas: cols[0]
                }],
                _loadTplsFull: [{
                    dom: 'esai120_edit', tpl: 'esai120_edit'
                },{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }]
            }, win);
        },
        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'esai120_table'),
                    url: g_ctxPath + 'crm/esai120/getList',
                    exportsName: moduleName + '数据',
                    title: moduleName + '表',
                    method: 'post',
                    id: 'esai120_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: false,
                    page: true,
                    where: table_where,
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-esai120setting-action').innerHTML).render({}, function (string) {
                    esai120SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(esai120_table)', function (obj) {
                    console.log(obj.data);

                    layer.tips(esai120SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            tipsIndex = index;
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '1':
                                        self._openTplFull({
                                            tag: 'esai120_edit',
                                            tagData: {
                                                obj : obj,
                                                moduleName : moduleName
                                            },
                                            index: 0,
                                            openOpts: {
                                                title: moduleName + '编辑',
                                                area: editArea
                                            }
                                        });
                                        break;
                                    case '2':
                                        app.confirm({
                                            msg: '是否删除选择的数据？',
                                            ok: function () {
                                                self.deleteData(obj);
                                            }
                                        });
                                        break;
                                }
                                layer.close(index);
                                tipsIndex = null;
                            });
                        }
                    });
                    return false;
                });

                //监听行双击事件
                table.on('rowDouble(esai120_table)', function (obj) {
                    self._openTplFull({
                        tag: 'esai120_edit',
                        tagData: {
                            obj : obj,
                            moduleName : moduleName
                        },
                        index: 0,
                        openOpts: {
                            title: moduleName + '编辑',
                            maxmin: true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(esai120_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'esai120_edit',
                                tagData: {
                                    is_add : true,
                                    moduleName : moduleName
                                },
                                index: 0,
                                openOpts: {
                                    title: moduleName + '编辑',
                                    maxmin: true
                                }
                            });
                            break;
                        case 'del':
                            app.confirm({
                                msg: '是否删除选择的数据？',
                                ok: function () {
                                    self.deleteDataBatch();
                                }
                            });
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },
        reloadTableCurr: function () {
            self._reloadTable({id: 'esai120_table', where: table_where});
        },
        deleteData: function (obj) {
            app.ajax({
                xurl: 'crm/esai120/del',
                xdata: obj.data,
                loading: true,
                okmsg: moduleName +'信息删除成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTableCurr();
                    }
                }
            });
        },
        deleteDataBatch: function(){
            var checkStatus = table.checkStatus('esai120_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('ESAB_T');
                    _temp.ESAB003 = v.ESAB003;
                    _xdata.push(_temp);
                });
                app.ajax({
                    xurl: 'crm/esai120/delBatch',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    okmsg: moduleName +'信息删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },
        _ready: function () {
            vueData.serchData = table_where;
            this._super({
                vue: [{
                    el: '#page',
                    id: 'page',
                    data: vueData,
                    searchId: 'esai120',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field === 'ESAB001') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwak001',
                                            columnList: [
                                                {label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.ESAB001 = data.GWAK001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '465px']
                                    }
                                });
                            } else if (field === 'ESAB003') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_sysuser_id',
                                            columnList: [
                                                {label: '人员编号', prop: 'ID', width: '150'},
                                                {label: '姓名', prop: 'NAME', width: '150'},
                                                {label: '账户', prop: 'ACCOUNT', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.ESAB003 = data.ID;
                                        },
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择主要联系人信息',
                                        area: ['600px', '470px']
                                    }
                                });
                            }else if (field === 'ESAB025') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_sysuser_id',
                                            columnList: [
                                                {label: '人员编号', prop: 'ID', width: '150'},
                                                {label: '姓名', prop: 'NAME', width: '150'},
                                                {label: '账户', prop: 'ACCOUNT', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.ESAB025 = data.ID;
                                        },
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择维保人员',
                                        area: ['600px', '470px']
                                    }
                                });
                            }
                        }
                    }
                }]
            });

            self.initTable();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

    return new esai120();

});